function [avgImg] = avgFolderImg(dirPath, fileType)

% READ IN IMAGES
if dirPath(end) == filesep
    dirPath = dirPath(1:end - 1);
end

fileNames = dir([dirPath filesep fileType]);
numImg = numel(fileNames);
I = imread(fileNames(1).name);
imgArray = zeros([size(I), numImg], class(I));
imgArray(:,:,1) = I;
for k = 2:numImg
    imgArray(:,:,k) = imread(fileNames(k).name);
end

%COMPUTE OVERLAPPING MASK
imgbwArray = (imgArray ~= 0);
for i = 2:numImg
    imgbwMask = immultiply(imgbwArray(:,:,1),imgbwArray(:,:,i));
end

%MULTIPLY MASK ONTO ALL IMAGES
%Must multiply by same class and will need to havethem all in double for
%averaging
imgbwMask = im2double(imgbwMask);
imgArray = im2double(imgArray);

imgcropArray = zeros([size(I), numImg]);
for k =1:numImg
    imgcropArray(:,:,k) = imgArray(:,:,k).*imgbwMask;
end

% SUM IMAGES WITHOUT SELECTING PIXEL BOUNDARIES
% I believe that summing them with or without ignoring black pixels will
% produce the same results for this script since all images have the same
% active pixels due to the mask they were multiplied by.

sumImg = imgArray(:,:,1);
for p = 2: numImg
    sumImg = imadd(sumImg, imgArray(:,:,p));
end

avgImg = sumImg/numImg;

end